package com.vinaprime.primeschedule.businesslayer;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.ParseException;

import com.vinaprime.primeschedule.databaselayer.VPSnoozeSound_DL;
import com.vinaprime.primeschedule.entities.VPSnoozeSound;
@SuppressLint("DefaultLocale")
public class VPSnoozeSound_BL {

	private VPSnoozeSound_DL mySoundDL;
	
	public VPSnoozeSound_BL(Context context, String path ){
		this.mySoundDL = new VPSnoozeSound_DL(context, path);
	}
	
	public int getNewSoundId(){
		int _id = 0;
		Cursor  cur =  mySoundDL.getNewSnoozeSoundID();
		if(cur.moveToFirst()){
			if(!cur.isNull(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbTableVpssId()))){
				_id = 	Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbTableVpssId())));
			}
		}
		return _id+1;
	}
	
	public VPSnoozeSound getSoundbyID(int _id){
		VPSnoozeSound sound = new VPSnoozeSound();
		Cursor cur  =  mySoundDL.getSnoozeSoundbyID(_id);
		int rowcount = cur.getCount();
		if (cur.moveToFirst()){
			for (int i = 0 ; i <=rowcount;i++){
				sound.setVpssId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbTableVpssId()))));
				sound.setVpssFilePath(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssFilePath())));
				sound.setVpssEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssEnableFlag())));
				sound.setVpssSound(cur.getBlob(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbvpssSound())));
				sound.setVpssSize(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssSize()))));
				cur.moveToNext();				
			}
		}
		return sound;
		
	}
	
	public List<VPSnoozeSound> getAllSound(){
		List<VPSnoozeSound> mySoundList = new ArrayList<VPSnoozeSound>(); 
		
		Cursor cur  =  this.mySoundDL.getAllSound();
		int rowcount = cur.getCount();
		if (cur.moveToFirst()){
			for (int i = 0 ; i <rowcount;i++){
				VPSnoozeSound sound = new VPSnoozeSound();
				sound.setVpssId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbTableVpssId()))));
				sound.setVpssFilePath(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssFilePath())));
				sound.setVpssEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssEnableFlag())));
				sound.setVpssSound(cur.getBlob(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbvpssSound())));
				sound.setVpssSize(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssSize()))));
				mySoundList.add(sound);
				cur.moveToNext();	
				
			}
		}
		return mySoundList;
	}
	
	public List<VPSnoozeSound> getAllSoundActived(){
		List<VPSnoozeSound> mySoundList = new ArrayList<VPSnoozeSound>(); 
		
		Cursor cur  =  this.mySoundDL.getAllSoundActived();
		int rowcount = cur.getCount();
		if (cur.moveToFirst()){
			for (int i = 0 ; i <rowcount;i++){
				VPSnoozeSound sound = new VPSnoozeSound();
				sound.setVpssId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbTableVpssId()))));
				sound.setVpssFilePath(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssFilePath())));
				sound.setVpssEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssEnableFlag())));
				sound.setVpssSound(cur.getBlob(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbvpssSound())));
				sound.setVpssSize(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPSnoozeSound_DL.getDbVpssSize()))));
				mySoundList.add(sound);
				cur.moveToNext();	
				
			}
		}
		return mySoundList;
	}

	@SuppressLint("DefaultLocale")
	public List<VPSnoozeSound> getSnoozeSoundbyContain(String _contain, List<VPSnoozeSound> _list) throws IllegalArgumentException, ParseException{
	//	System.out.print("Size asddddddddd: "+_list);
		if (_contain ==""||_contain == null){
			return _list;
		}else{
			List<VPSnoozeSound> listsound =  new ArrayList<VPSnoozeSound>();
			int rownum = _list.size();			
		//	System.out.print("Size aaaA: "+rownum);
			for (int i = 0; i<rownum; i++){
				
				String str = _list.get(i).getVpssFilePath();
				if (str.toLowerCase().contains(_contain.toLowerCase())){
					VPSnoozeSound sound = new VPSnoozeSound();
					sound = _list.get(i);
					listsound.add(sound);
				}
							
			}
			return listsound;
		}
	}
	
	public void addSound(VPSnoozeSound sound){
		this.mySoundDL.addSnoozeSound(sound);
	}
	
	public void updateSound(VPSnoozeSound sound){
		this.mySoundDL.updateSnoozeSound(sound);
	}
	
	public void deleteSoundbyID (int _id){
		this.mySoundDL.deleteSnoozeSoundbyId(_id);
	}
	
	public void deleteSound (VPSnoozeSound sound){
		this.mySoundDL.deleteSnoozeSound(sound);
	}
	
	public void updateSound_EnableFlag(VPSnoozeSound sound){
		this.mySoundDL.updateSnoozeSound(sound);
	}
}
